* ---------------------------------------------------------------------------------------------------
* Code file to reproduce main regressions from "Trade, Policy, and Development in the Digital Economy"
* 		
*     This file produces the regression results presented in tables 2 through 6.
*	  It requires two files as data inputs:
*			1. aggregate_trade_data.dta 
*			2. goods_servoces_trade_data.dta
* 
* 	  Created by by Peter Herman and Sarah Oliver (2023)
* ---------------------------------------------------------------------------------------------------
clear
set maxvar 120000
cd "D:\work\Peter_Herman\projects\internet_and_trade_public_files"
capture log close
local logdate =  string(date(c(current_date), "DMY"), "%tdCCYYNNDD") + subinstr("_`c(current_time)'", ":", "", .)
log using "main_regressions_`logdate'", text




* ------------------------
*
* Step 1: Aggregate Trade Analysis (tables 2, 3, and 5)
*
* ------------------------


* --------------
* Prepare aggregate trade 
* --------------
use "aggregate_trade_data", clear 

* Prepare Colony and Distance measures
generate colony_ever = colony_of_destination_ever+colony_of_origin_ever
generate log_distance=log(distance)


* Prepare bandwidth measures 
* Bandwidth from ITU reported in kbits/s, Convert to mbit/s
replace minimum_bandwidth=minimum_bandwidth/1000 
* Generate low, medium, and high bandwidth categories
generate bandwidth_cat=0
replace bandwidth_cat=1 if minimum_bandwidth>=1 & minimum_bandwidth<3
replace bandwidth_cat=2 if minimum_bandwidth>=3
tab bandwidth_cat, gen(bandwidth_cat_)
egen max_bandwidth=max(minimum_bandwidth)
generate bandwidth_index = minimum_bandwidth/max_bandwidth


* Generate variable that indicates high income importer (both high and upper middle income)
generate high_income_exp=0
* Upper income
replace high_income_exp=1 if exp_iso3=="AND" | exp_iso3=="ATG" | exp_iso3=="ABW" | exp_iso3=="AUS" | exp_iso3=="AUT" | exp_iso3=="BHS" | exp_iso3=="BHR" | exp_iso3=="BRB" | exp_iso3=="BEL" | exp_iso3=="BMU" | exp_iso3=="VGB" | exp_iso3=="BRN" | exp_iso3=="CAN" | exp_iso3=="CYM" | exp_iso3=="CHL" | exp_iso3=="HRV" | exp_iso3=="CUW" | exp_iso3=="CYP" | exp_iso3=="CZE" | exp_iso3=="DNK" | exp_iso3=="EST" | exp_iso3=="FRO" | exp_iso3=="FIN" | exp_iso3=="FRA" | exp_iso3=="PYF" | exp_iso3=="DEU" | exp_iso3=="GIB" | exp_iso3=="GRC" | exp_iso3=="GRL" | exp_iso3=="GUM" | exp_iso3=="HKG" | exp_iso3=="HUN" | exp_iso3=="ISL" | exp_iso3=="IRL" | exp_iso3=="IMN" | exp_iso3=="ISR" | exp_iso3=="ITA" | exp_iso3=="JPN" | exp_iso3=="KOR" | exp_iso3=="KWT" | exp_iso3=="LVA" | exp_iso3=="LIE" | exp_iso3=="LTU" | exp_iso3=="LUX" | exp_iso3=="MAC" | exp_iso3=="MLT" | exp_iso3=="MCO" | exp_iso3=="NRU" | exp_iso3=="NLD" | exp_iso3=="NCL" | exp_iso3=="NZL" | exp_iso3=="MNP" | exp_iso3=="NOR" | exp_iso3=="OMN" | exp_iso3=="PLW" | exp_iso3=="POL" | exp_iso3=="PRT" | exp_iso3=="PRI" | exp_iso3=="QAT" | exp_iso3=="SMR" | exp_iso3=="SAU" | exp_iso3=="SYC" | exp_iso3=="SGP" | exp_iso3=="SXM" | exp_iso3=="SVK" | exp_iso3=="SVN" | exp_iso3=="ESP" | exp_iso3=="KNA" | exp_iso3=="MAF" | exp_iso3=="SWE" | exp_iso3=="CHE" | exp_iso3=="TWN" | exp_iso3=="TTO" | exp_iso3=="TCA" | exp_iso3=="ARE" | exp_iso3=="GBR" | exp_iso3=="USA" | exp_iso3=="URY" | exp_iso3=="VIR"  | exp_iso3=="ROU" | exp_iso3=="PAN"
*upper middle income
replace high_income_exp=1 if exp_iso3=="ALB" | exp_iso3=="FJI" | exp_iso3=="NAM" | exp_iso3=="ASM" | exp_iso3=="GAB" | exp_iso3=="MKD" | exp_iso3=="ARG" | exp_iso3=="GEO" | exp_iso3=="PLW" | exp_iso3=="ARM" | exp_iso3=="GRD" | exp_iso3=="PRY" | exp_iso3=="AZE" | exp_iso3=="GTM" | exp_iso3=="PER" | exp_iso3=="BLR" | exp_iso3=="GUY" | exp_iso3=="RUS" | exp_iso3=="BLZ" | exp_iso3=="IRQ" | exp_iso3=="SRB" | exp_iso3=="BIH" | exp_iso3=="JAM" | exp_iso3=="ZAF" | exp_iso3=="BWA" | exp_iso3=="JOR" | exp_iso3=="LCA" | exp_iso3=="BRA" | exp_iso3=="KAZ" | exp_iso3=="VCT" | exp_iso3=="BLG" | exp_iso3=="KSV" | exp_iso3=="SUR" | exp_iso3=="CHN" | exp_iso3=="LBY" | exp_iso3=="THA" | exp_iso3=="COL" | exp_iso3=="MYS" | exp_iso3=="TON" | exp_iso3=="CRI" | exp_iso3=="MDV" | exp_iso3=="TUR" | exp_iso3=="CUB" | exp_iso3=="MHL" | exp_iso3=="TKM" | exp_iso3=="DMA" | exp_iso3=="MUS" | exp_iso3=="TUV" | exp_iso3=="DOM" | exp_iso3=="MEX" | exp_iso3=="GNQ" | exp_iso3=="MDA" | exp_iso3=="ECU" | exp_iso3=="MNE" 

* Generate indicator for lower income exporter
generate low_inc_exp=0
replace low_inc_exp=1 if high_income_exp==0

* Generate high-/low-income interactions with internet use measure
gen high_int_ex=high_income_exp*internet_use_pair
gen low_int_ex = low_inc_exp*internet_use_pair


* --- Prepare digital provision measures
* Convert measures that are more than just 0/1 to 0/1 indicators
foreach v of var tech_neutrality- free_data_flow{
	replace `v' = 1 if `v'>0
}

* Create provision indicator
generate any_digital = 1 if free_data_flow==1 | tech_neutrality==1 | ec_duty_prohibit==1 | electronic_sig==1 | data_protection==1 | cybersecurity==1 | goods_big_data==1
replace any_digital = 0 if any_digital==.

* Create provision index
generate provision_count=free_data_flow+tech_neutrality+ec_duty_prohibit+electronic_sig+data_protection+cybersecurity+goods_big_data
replace provision_count=0 if provision_count==.
generate provision_index = provision_count/7

* Generate provision x income interactions
gen high_digital_ex=high_income_exp*provision_index
gen low_digital_ex=low_inc_exp*provision_index
gen high_any_dig_ex=high_income_exp*any_digital
gen low_any_dig_ex=low_inc_exp*any_digital

* Create a constant
gen constant = 1


* Generate internet use foregn x domestic interactions
generate int_internet = internet_use_pair*foreign
generate dom_internet = internet_use_pair*(1-foreign)
gen frgn_high_int_ex = high_int_ex*foreign
gen dom_high_int_ex = high_int_ex*(1-foreign)
gen frgn_low_int_ex = low_int_ex*foreign
gen dom_low_int_ex = low_int_ex*(1-foreign)

* Generate internet use quartiles
xtile int_quartiles=internet_use_pair, nquantiles(4)
tab int_quartiles, gen(int_cat_)

* Add symetric pair identifiers
encode pair_sym, generate(sym_pair)



* -----
* Table 2 - Digital Connectivity-Total Trade
* -----
eststo clear

* Baseline
eststo: ppmlhdfe trade agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.exporter#i.importer) cluster(i.importer#i.exporter)

* Basic Connectivity + bandwidth
eststo: ppmlhdfe trade agree_pta member_eu_joint internet_use_pair bandwidth_index foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.exporter#i.importer) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[internet_use_pair]*.1636218)-1)
nlcom 100*(exp(_b[bandwidth_index]*.0042388)-1)
nlcom 100*(exp(_b[bandwidth_index])-1)


* Internet use x international/domestic trade
eststo: ppmlhdfe trade int_internet dom_internet bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.exporter#i.importer) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[dom_internet]*.1636218)-1)
nlcom 100*(exp(_b[int_internet]*.1636218)-1)
nlcom 100*(exp(_b[bandwidth_index]*.0042388)-1)
nlcom 100*(exp(_b[bandwidth_index])-1)
test _b[int_internet] = _b[dom_internet]


* Quartiles and high/low bandwidth
eststo: ppmlhdfe  trade int_cat_2 int_cat_3 int_cat_4 bandwidth_cat_2 bandwidth_cat_3 agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[int_cat_2]*.1636218)-1)
nlcom 100*(exp(_b[int_cat_3]*.1636218)-1)
nlcom 100*(exp(_b[int_cat_4]*.1636218)-1)
nlcom 100*(exp(_b[bandwidth_cat_2])-1)
nlcom 100*(exp(_b[bandwidth_cat_3])-1)
test _b[bandwidth_cat_2] = _b[bandwidth_cat_3]


* internet use x high or low income exporter
eststo: ppmlhdfe  trade high_int_ex low_int_ex bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[high_int_ex]*.1636218)-1)
nlcom 100*(exp(_b[low_int_ex]*.1636218)-1)
test _b[high_int_ex] = _b[low_int_ex]


* internet use x high or low income exporter + pair trend
eststo: ppmlhdfe  trade high_int_ex low_int_ex bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.sym_pair#c.year) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[high_int_ex]*.1636218)-1)
nlcom 100*(exp(_b[low_int_ex]*.1636218)-1)
test _b[high_int_ex] = _b[low_int_ex]


local logdate =  string(date(c(current_date), "DMY"), "%tdCCYYNNDD") + subinstr("_`c(current_time)'", ":", "", .)
esttab using "table2_`logdate'.csv", star(* 0.10 ** 0.05 *** 0.01) se aic scalars(rmse r2_p)  replace


* ------
* Table 3 - Cables
* ------
eststo clear

*add submarine cables-drop landlocked countries
eststo: ppmlhdfe  trade int_internet dom_internet bandwidth_index undersea_c agree_pta member_eu_joint foreign_2001-foreign_2016 if landlocked_d!=1, absorb(i.importer#i.year i.exporter#i.year i.exporter#i.importer) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[dom_internet]*.1636218)-1)
nlcom 100*(exp(_b[int_internet]*.1636218)-1)
nlcom 100*(exp(_b[bandwidth_index]*.0042388)-1)
nlcom 100*(exp(_b[bandwidth_index])-1)
nlcom 100*(exp(_b[undersea_c])-1)
test _b[int_internet] = _b[dom_internet]



*internet use and bandwidth index-no pair FE
eststo: ppmlhdfe  trade int_internet dom_internet bandwidth_index log_distance colony_ever common_language contiguity agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[dom_internet]*.1636218)-1)
nlcom 100*(exp(_b[int_internet]*.1636218)-1)
nlcom 100*(exp(_b[bandwidth_index]*.0042388)-1)
nlcom 100*(exp(_b[bandwidth_index])-1)
test _b[int_internet] = _b[dom_internet]



*add submarine and overland cables-no pair FE
eststo: ppmlhdfe  trade int_internet dom_internet bandwidth_index undersea_c overland_c log_distance colony_ever common_language contiguity agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[dom_internet]*.1636218)-1)
nlcom 100*(exp(_b[int_internet]*.1636218)-1)
nlcom 100*(exp(_b[bandwidth_index]*.0042388)-1)
nlcom 100*(exp(_b[bandwidth_index])-1)
nlcom 100*(exp(_b[undersea_c])-1)
nlcom 100*(exp(_b[overland_c])-1)
test _b[int_internet] = _b[dom_internet]

local logdate =  string(date(c(current_date), "DMY"), "%tdCCYYNNDD") + subinstr("_`c(current_time)'", ":", "", .)
esttab using "table3_`logdate'.csv", star(* 0.10 ** 0.05 *** 0.01) se aic scalars(rmse r2_p)  replace


* -------
* Table 5 - Provisions + Aggregate Trade
* --------
eststo clear

* Internet Use foreign/domestic + "any" provisions
eststo clear
eststo: ppmlhdfe  trade any_digital int_internet dom_internet bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.exporter#i.importer) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[int_internet]*.1636218)-1)
nlcom 100*(exp(_b[dom_internet]*.1636218)-1)
nlcom 100*(exp(_b[bandwidth_index]*.0042388)-1)
nlcom 100*(exp(_b[bandwidth_index])-1)
nlcom 100*(exp(_b[any_digital])-1)
test _b[int_internet] = _b[dom_internet]


* Internet Use foreign/domestic + provision index
eststo: ppmlhdfe  trade provision_index int_internet dom_internet bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.exporter#i.importer) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[int_internet]*.1636218)-1)
nlcom 100*(exp(_b[dom_internet]*.1636218)-1)
nlcom 100*(exp(_b[bandwidth_index]*.0042388)-1)
nlcom 100*(exp(_b[bandwidth_index])-1)
nlcom 100*(exp(_b[provision_index])-1)
test _b[int_internet] = _b[dom_internet]


* Internet use + "any" provisions, both high/low income
eststo: ppmlhdfe  trade high_int_ex low_int_ex high_any_dig_ex low_any_dig_ex  bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.exporter#i.importer) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[high_int_ex]*.1636218)-1)
nlcom 100*(exp(_b[low_int_ex]*.1636218)-1)
nlcom 100*(exp(_b[bandwidth_index]*.0042388)-1)
nlcom 100*(exp(_b[bandwidth_index])-1)
nlcom 100*(exp(_b[high_any_dig_ex])-1)
nlcom 100*(exp(_b[low_any_dig_ex])-1)
test _b[high_int_ex] = _b[low_int_ex]
test _b[high_any_dig_ex] = _b[low_any_dig_ex]

* Internet use + provision index, both high/low income
eststo: ppmlhdfe  trade high_int_ex low_int_ex high_digital_ex low_digital_ex  bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.exporter#i.importer) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[high_int_ex]*.1636218)-1)
nlcom 100*(exp(_b[low_int_ex]*.1636218)-1)
nlcom 100*(exp(_b[bandwidth_index]*.0042388)-1)
nlcom 100*(exp(_b[bandwidth_index])-1)
nlcom 100*(exp(_b[high_digital_ex])-1)
nlcom 100*(exp(_b[low_digital_ex])-1)
test _b[high_int_ex] = _b[low_int_ex]
test _b[high_digital_ex] = _b[low_digital_ex]


* Internet use + "any" provisions, both high/low income, pair trend
eststo: ppmlhdfe  trade high_int_ex low_int_ex high_any_dig_ex low_any_dig_ex  bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.sym_pair#c.year) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[high_int_ex]*.1636218)-1)
nlcom 100*(exp(_b[low_int_ex]*.1636218)-1)
nlcom 100*(exp(_b[bandwidth_index]*.0042388)-1)
nlcom 100*(exp(_b[bandwidth_index])-1)
nlcom 100*(exp(_b[high_any_dig_ex])-1)
nlcom 100*(exp(_b[low_any_dig_ex])-1)
test _b[high_int_ex] = _b[low_int_ex]
test _b[ high_any_dig_ex] = _b[low_any_dig_ex]


local logdate =  string(date(c(current_date), "DMY"), "%tdCCYYNNDD") + subinstr("_`c(current_time)'", ":", "", .)
esttab using "table5_`logdate'.csv", star(* 0.10 ** 0.05 *** 0.01) se aic scalars(rmse r2_p)  replace



* -----
* Aggregate Data for simulation
* -----


eststo clear
eststo: ppml_panel_sg  trade high_any_dig_ex low_any_dig_ex  high_int_ex low_int_ex bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016 constant, exporter(exporter) importer(importer) year(year) genD(pairfe_all_provisions) verbose(100) maxiter(100000)

predict trade_prediction
generate int_provisions_resid=trade-trade_prediction

estout using "betas_othervars_aggregate.csv", cells(b se) replace

preserve
keep trade year importer exporter high_digital_ex low_digital_ex high_any_dig_ex low_any_dig_ex high_int_ex low_int_ex internet_use_pair provision_index bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016 constant
export delimited using "all_xvalues_aggregate.csv", replace
restore

preserve
collapse  pairfe_all_provisions, by(importer exporter)
export delimited using "all_countrypairFE_aggregate.csv", replace
restore





* -----------------------------
*
* Step 2: Goods and Services Analysis (tables 4 and 6)
*
* ----------------------------

* -----
* Prep goods and services data
* -----

use "goods_services_trade_data.dta", clear 

* Prepare Colony and Distance measures
generate colony_ever = colony_of_destination_ever+colony_of_origin_ever
generate log_distance=log(distance)

* Prepare bandwidth measures 
* Bandwidth from ITU reported in kbits/s, Convert to mbit/s
replace minimum_bandwidth=minimum_bandwidth/1000
* Generate low, medium, and high bandwidth categories
generate bandwidth_cat=0
replace bandwidth_cat=1 if minimum_bandwidth>=1 & minimum_bandwidth<3
replace bandwidth_cat=2 if minimum_bandwidth>=3
tab bandwidth_cat, gen(bandwidth_cat_)

egen max_bandwidth=max(minimum_bandwidth)
generate bandwidth_index = minimum_bandwidth/max_bandwidth

// decode importer, generate(imp_iso3)
// decode exporter, generate(exp_iso3)

*Generate variable that indicates high income importer (both high and upper middle income)
generate high_income_exp=0
* Upper income
replace high_income_exp=1 if exp_iso3=="AND" | exp_iso3=="ATG" | exp_iso3=="ABW" | exp_iso3=="AUS" | exp_iso3=="AUT" | exp_iso3=="BHS" | exp_iso3=="BHR" | exp_iso3=="BRB" | exp_iso3=="BEL" | exp_iso3=="BMU" | exp_iso3=="VGB" | exp_iso3=="BRN" | exp_iso3=="CAN" | exp_iso3=="CYM" | exp_iso3=="CHL" | exp_iso3=="HRV" | exp_iso3=="CUW" | exp_iso3=="CYP" | exp_iso3=="CZE" | exp_iso3=="DNK" | exp_iso3=="EST" | exp_iso3=="FRO" | exp_iso3=="FIN" | exp_iso3=="FRA" | exp_iso3=="PYF" | exp_iso3=="DEU" | exp_iso3=="GIB" | exp_iso3=="GRC" | exp_iso3=="GRL" | exp_iso3=="GUM" | exp_iso3=="HKG" | exp_iso3=="HUN" | exp_iso3=="ISL" | exp_iso3=="IRL" | exp_iso3=="IMN" | exp_iso3=="ISR" | exp_iso3=="ITA" | exp_iso3=="JPN" | exp_iso3=="KOR" | exp_iso3=="KWT" | exp_iso3=="LVA" | exp_iso3=="LIE" | exp_iso3=="LTU" | exp_iso3=="LUX" | exp_iso3=="MAC" | exp_iso3=="MLT" | exp_iso3=="MCO" | exp_iso3=="NRU" | exp_iso3=="NLD" | exp_iso3=="NCL" | exp_iso3=="NZL" | exp_iso3=="MNP" | exp_iso3=="NOR" | exp_iso3=="OMN" | exp_iso3=="PLW" | exp_iso3=="POL" | exp_iso3=="PRT" | exp_iso3=="PRI" | exp_iso3=="QAT" | exp_iso3=="SMR" | exp_iso3=="SAU" | exp_iso3=="SYC" | exp_iso3=="SGP" | exp_iso3=="SXM" | exp_iso3=="SVK" | exp_iso3=="SVN" | exp_iso3=="ESP" | exp_iso3=="KNA" | exp_iso3=="MAF" | exp_iso3=="SWE" | exp_iso3=="CHE" | exp_iso3=="TWN" | exp_iso3=="TTO" | exp_iso3=="TCA" | exp_iso3=="ARE" | exp_iso3=="GBR" | exp_iso3=="USA" | exp_iso3=="URY" | exp_iso3=="VIR"  | exp_iso3=="ROU" | exp_iso3=="PAN"
*upper middle income
replace high_income_exp=1 if exp_iso3=="ALB" | exp_iso3=="FJI" | exp_iso3=="NAM" | exp_iso3=="ASM" | exp_iso3=="GAB" | exp_iso3=="MKD" | exp_iso3=="ARG" | exp_iso3=="GEO" | exp_iso3=="PLW" | exp_iso3=="ARM" | exp_iso3=="GRD" | exp_iso3=="PRY" | exp_iso3=="AZE" | exp_iso3=="GTM" | exp_iso3=="PER" | exp_iso3=="BLR" | exp_iso3=="GUY" | exp_iso3=="RUS" | exp_iso3=="BLZ" | exp_iso3=="IRQ" | exp_iso3=="SRB" | exp_iso3=="BIH" | exp_iso3=="JAM" | exp_iso3=="ZAF" | exp_iso3=="BWA" | exp_iso3=="JOR" | exp_iso3=="LCA" | exp_iso3=="BRA" | exp_iso3=="KAZ" | exp_iso3=="VCT" | exp_iso3=="BLG" | exp_iso3=="KSV" | exp_iso3=="SUR" | exp_iso3=="CHN" | exp_iso3=="LBY" | exp_iso3=="THA" | exp_iso3=="COL" | exp_iso3=="MYS" | exp_iso3=="TON" | exp_iso3=="CRI" | exp_iso3=="MDV" | exp_iso3=="TUR" | exp_iso3=="CUB" | exp_iso3=="MHL" | exp_iso3=="TKM" | exp_iso3=="DMA" | exp_iso3=="MUS" | exp_iso3=="TUV" | exp_iso3=="DOM" | exp_iso3=="MEX" | exp_iso3=="GNQ" | exp_iso3=="MDA" | exp_iso3=="ECU" | exp_iso3=="MNE"  

* Generate indicator for lower income exporter
generate low_inc_exp=0
replace low_inc_exp=1 if high_income_exp==0

* Generate high-/low-income interactions with internet use measure
gen high_int_ex=high_income_exp*internet_use_pair
gen low_int_ex = low_inc_exp*internet_use_pair

* --- Prepare digital provision measures
* Convert measures that are more than just 0/1 to 0/1 indicators
foreach v of var tech_neutrality-free_data_flow{
	replace `v' = 1 if `v'>0
}

* Create provision indicator
generate any_digital = 1 if free_data_flow==1 | tech_neutrality==1 | ec_duty_prohibit==1 | electronic_sig==1 | data_protection==1 | cybersecurity==1 | goods_big_data==1
replace any_digital = 0 if any_digital==.

* Create provision index
generate provision_count=free_data_flow+tech_neutrality+ec_duty_prohibit+electronic_sig+data_protection+cybersecurity+goods_big_data
replace provision_count=0 if provision_count==.
generate provision_index = provision_count/7

* Generate provision x income interactions
gen high_digital_ex=high_income_exp*provision_index
gen low_digital_ex=low_inc_exp*provision_index
gen high_any_dig_ex=high_income_exp*any_digital
gen low_any_dig_ex=low_inc_exp*any_digital

* Create a constant
gen constant = 1


* Generate internet use foregn x domestic interactions
generate int_internet = internet_use_pair*foreign
generate dom_internet = internet_use_pair*(1-foreign)
gen frgn_high_int_ex = high_int_ex*foreign
gen dom_high_int_ex = high_int_ex*(1-foreign)
gen frgn_low_int_ex = low_int_ex*foreign
gen dom_low_int_ex = low_int_ex*(1-foreign)


* Encode pair trend
encode pair_sym, generate(sym_pair)

encode goods_svcs, generate(sector)

* --------------
* Table 4 - Goods/Services Connectivity
* --------------

 eststo clear
forvalues s=1/2 {


* Foreign/domestic internet use
eststo: ppmlhdfe  trade int_internet dom_internet bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016 if sector==`s', absorb(i.importer#i.year i.exporter#i.year i.exporter#i.importer) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[int_internet]*.1636218)-1)
nlcom 100*(exp(_b[dom_internet]*.1636218)-1)
nlcom 100*(exp(_b[bandwidth_index]*.0042388)-1)
nlcom 100*(exp(_b[bandwidth_index])-1)
test _b[int_internet] = _b[dom_internet]


* High Income/low income internet use
eststo: ppmlhdfe  trade high_int_ex low_int_ex bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016 if sector==`s', absorb(i.importer#i.year i.exporter#i.year i.exporter#i.importer) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[high_int_ex]*.1636218)-1)
nlcom 100*(exp(_b[low_int_ex]*.1636218)-1)
nlcom 100*(exp(_b[bandwidth_index]*.0042388)-1)
nlcom 100*(exp(_b[bandwidth_index])-1)
test _b[high_int_ex] = _b[low_int_ex]

 
 
* High Income/low income internet use, pair trend
eststo: ppmlhdfe  trade high_int_ex low_int_ex bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016 if sector==`s', absorb(i.importer#i.year i.exporter#i.year i.sym_pair#c.year) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[high_int_ex]*.1636218)-1)
nlcom 100*(exp(_b[low_int_ex]*.1636218)-1)
nlcom 100*(exp(_b[bandwidth_index]*.0042388)-1)
nlcom 100*(exp(_b[bandwidth_index])-1)
test _b[high_int_ex] = _b[low_int_ex]

}

local logdate =  string(date(c(current_date), "DMY"), "%tdCCYYNNDD") + subinstr("_`c(current_time)'", ":", "", .)
esttab using "table4_`logdate'.csv", star(* 0.10 ** 0.05 *** 0.01) se aic scalars(rmse r2_p)  replace




* --------------
* Table 6 - Goods/Services Connectivity-Total
* --------------

 eststo clear
forvalues s=1/2 {
* High Income/low income internet use + High/low digital provision index
eststo: ppmlhdfe  trade high_int_ex low_int_ex high_digital_ex low_digital_ex bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016 if sector==`s', absorb(i.importer#i.year i.exporter#i.year i.exporter#i.importer) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[high_int_ex]*.1636218)-1)
nlcom 100*(exp(_b[low_int_ex]*.1636218)-1)
nlcom 100*(exp(_b[bandwidth_index]*.0042388)-1)
nlcom 100*(exp(_b[bandwidth_index])-1)
nlcom 100*(exp(_b[high_digital_ex])-1)
nlcom 100*(exp(_b[low_digital_ex])-1)
nlcom 100*(exp(_b[high_digital_ex]*1/7)-1)
nlcom 100*(exp(_b[low_digital_ex]*1/7)-1)
test _b[high_int_ex] = _b[low_int_ex]
test _b[high_digital_ex] = _b[low_digital_ex]



* High Income/low income internet use + High/low "any" digital provision
eststo: ppmlhdfe  trade high_int_ex low_int_ex high_any_dig_ex low_any_dig_ex  bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016 if sector==`s', absorb(i.importer#i.year i.exporter#i.year i.exporter#i.importer) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[high_int_ex]*.1636218)-1)
nlcom 100*(exp(_b[low_int_ex]*.1636218)-1)
nlcom 100*(exp(_b[bandwidth_index]*.0042388)-1)
nlcom 100*(exp(_b[bandwidth_index])-1)
nlcom 100*(exp(_b[high_any_dig_ex])-1)
nlcom 100*(exp(_b[low_any_dig_ex])-1)
test _b[high_int_ex] = _b[low_int_ex]
test _b[high_any_dig_ex] = _b[low_any_dig_ex]




* High Income/low income internet use + High/low "any" digital provision
eststo: ppmlhdfe  trade high_int_ex low_int_ex high_any_dig_ex low_any_dig_ex  bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016 if sector==`s', absorb(i.importer#i.year i.exporter#i.year i.sym_pair#c.year) cluster(i.importer#i.exporter)
nlcom 100*(exp(_b[high_int_ex]*.1636218)-1)
nlcom 100*(exp(_b[low_int_ex]*.1636218)-1)
nlcom 100*(exp(_b[bandwidth_index]*.0042388)-1)
nlcom 100*(exp(_b[bandwidth_index])-1)
nlcom 100*(exp(_b[high_any_dig_ex])-1)
nlcom 100*(exp(_b[low_any_dig_ex])-1)
test _b[high_int_ex] = _b[low_int_ex]
test _b[high_any_dig_ex] = _b[low_any_dig_ex]



}

local logdate =  string(date(c(current_date), "DMY"), "%tdCCYYNNDD") + subinstr("_`c(current_time)'", ":", "", .)
esttab using "table6_`logdate'.csv", star(* 0.10 ** 0.05 *** 0.01) se aic scalars(rmse r2_p)  replace





* -----
*  Generate Fixed effect estimates for services simulation
* -----

capture log close

* Drop goods trade
keep if sector==2

eststo clear

* Rerun with ppml_panel_sg in order to generate 
eststo: ppml_panel_sg trade high_int_ex low_int_ex high_digital_ex low_digital_ex bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016 constant if sector==2, exporter(exporter) importer(importer) year(year) genD(pairfe_all_provisions) verbose(1000) maxiter(100000)

predict trade_prediction
generate int_provisions_resid=trade-trade_prediction


preserve
collapse  pairfe_all_provisions, by(importer exporter)
export delimited using "all_countrypairFE_services_`logdate'.csv", replace
restore
